package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.jxzs.HtmlActivity;
import gl.common.UserManger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class PsychTestListFragment extends BaseListFragmet {

	private TextView empty;
	private ListView list;
	String NOTFOUND = "目前没有什么内容";
	private Drawable drawable;

	private void bindViews() {

		empty = (TextView) findViewById(R.id.empty);
		list = (ListView) findViewById(android.R.id.list);
		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});

		empty.setText(NOTFOUND);

		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public static PsychTestListFragment newInstance() {
		PsychTestListFragment fragment = new PsychTestListFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public PsychTestListFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container, false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_discovery_pic_btn3);
		bindViews();
		initdata();
		return rootView;
	}

	private void initdata() {
		setListViewDividerHeight(4);
		initAbsListView(getListView(), API.URL + "GetPsychtest?userid=%s",
				new Object[] { UserManger.getInstance().userid },
				R.layout.item_list_love, new String[] { "title", "thumbfiles",
						"content" }, new int[] { R.id.tv_nick, R.id.iv_avator,
						R.id.age });

	}

	@Override
	protected String preFormatJsonData(String t) {
		try {
			final JSONObject jsonObject = new JSONObject(t);
			JSONArray mja = jsonObject.getJSONArray("data");
			for (int i = 0; i < mja.length(); i++) {
				mja.getJSONObject(i).put("results", ""); 
			}
			return jsonObject.put("data", mja.toString()).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.preFormatJsonData(t);
	}

	@Override
	public Object getOnItemListener(String[] from, String t)
			throws JSONException {
		return new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				Tip.show("此功能将使用HTML5技术实现,请等待上线");
				
				JSONArray mja = dataJSONArray;
				String ceid;
				try {
					ceid = mja.getJSONObject(position).optString("ceid");
					String url = "http://116.211.116.168:8080/newp/src/index.html?cid="+ceid;
					final Intent intent = new Intent(getActivity(), HtmlActivity.class);
					intent.putExtra(HtmlActivity.KEY_URL, url);
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		};
	}

}